package org.example.slidingWindow;

/**
 * @author liyishan
 * @date 2023/11/7 09:26
 * @apiNote
 */

public class Test01031MaxSumTwoNoOverlap {

    public static void main(String[] args) {
        int[] nums = {0,6,5,2,2,5,1,9,4};
        int count = test(nums,1,2);
        System.out.println(count);
    }

    public static int test(int[] nums, int firstLen, int secondLen) {
        return Math.max(help(nums,firstLen,secondLen),help(nums,secondLen,firstLen));
    }

    private static int help(int[] nums,int firstLen,int secondLen){
        int sumL = 0;
        for(int i = 0;i<firstLen;i++){
            sumL+=nums[i];
        }
        int maxSumL = sumL;
        int sumR = 0;
        for(int i = firstLen;i<firstLen+secondLen;i++){
            sumR+=nums[i];
        }
        int res = maxSumL+sumR;
        for(int i = firstLen+secondLen,j=firstLen;i<nums.length;i++,j++){
            sumL +=nums[j]- nums[j-firstLen];
            maxSumL = Math.max(maxSumL,sumL);
            sumR += nums[i] - nums[i-secondLen];
            res = Math.max(res,maxSumL+sumR);
        }
        return res;
    }
}
